package com.icez.calendar_view

import android.content.Context
import android.graphics.Typeface
import android.util.TypedValue
import android.widget.RelativeLayout
import com.icez.base_util_library.adapter.CalendarViewAdapter
import com.icez.calendar_view.databinding.ItemCalendarDayBinding

class CalendarDayAdapter(
    layoutResId: Int,
    mData: ArrayList<CalendarModel.MonthDayModel>,
    val mYear: String,
    val mMonth: String,
    val mCalendarViewConfig: CalendarViewConfig,
    val mOnItemClickListener: (day:String?) -> Unit,
    val mOnRangeClickListener: ()->Unit
) : CalendarViewAdapter<CalendarModel.MonthDayModel, ItemCalendarDayBinding>(
    layoutResId,
    mData
) {
    override fun onBusinessOperation(
        context: Context,
        binding: ItemCalendarDayBinding,
        position: Int,
        item: CalendarModel.MonthDayModel?
    ) {
        binding.itemCalendarDay.text = item?.day
        binding.itemCalendarDay.setTextSize(TypedValue.COMPLEX_UNIT_PX,mCalendarViewConfig.mDayTextSize)
        if (mCalendarViewConfig.mDayTextIsBold) {
            binding.itemCalendarDay.setTypeface(Typeface.DEFAULT_BOLD)
        }

        binding.itemCalendarDay.setTextColor(mCalendarViewConfig.mDayTextColor)

        if (mCalendarViewConfig.mDayTextHeight > 0) {
            val layoutParams = RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                mCalendarViewConfig.mDayTextHeight
            )
            layoutParams.topMargin = mCalendarViewConfig.mDayTextPaddingTopBottom
            layoutParams.bottomMargin = mCalendarViewConfig.mDayTextPaddingTopBottom
            binding.itemCalendarDay.layoutParams = layoutParams
        }





        binding.itemCalendarDay?.setOnClickListener {
            if (!item?.day.isNullOrEmpty()) {
                val selectTimestamp =
                    Utils.dateStrToTimestamp("${mYear}-${mMonth}-${item?.day}", "yyyy-MM-dd")
                mOnItemClickListener(item?.day)
                if(mCalendarViewConfig.mIsOpenRange){
                    if (mCalendarViewConfig.startRangeDate <= 0) {
                        if (mCalendarViewConfig.mIsLmitStartRangeCurrentDate) {
                            val currentTime = Utils.dateStrToTimestamp(
                                "${Utils.currentYear()}-${Utils.currentMonth()}-${Utils.currentDay()}",
                                "yyyy-MM-dd"
                            )
                            if (selectTimestamp >= currentTime) {
                                mCalendarViewConfig.startRangeDate = selectTimestamp
                                mOnRangeClickListener()
                            }
                        } else {
                            mCalendarViewConfig.startRangeDate = selectTimestamp
                            mOnRangeClickListener()
                        }

                    } else {
                        if (mCalendarViewConfig.mIsLmitRangeNext) {
                            if (Utils.diffDayForTimestamp(
                                    mCalendarViewConfig.startRangeDate.toString(),
                                    selectTimestamp.toString()
                                ) < mCalendarViewConfig.mMaxRangeDayNum && mCalendarViewConfig.startRangeDate <= selectTimestamp
                            ) {
                                mCalendarViewConfig.endRangeDate = selectTimestamp

                                mOnRangeClickListener()
                            }
                        } else {
                            if (Utils.diffDayForTimestamp(
                                    mCalendarViewConfig.startRangeDate.toString(),
                                    selectTimestamp.toString()
                                ) < mCalendarViewConfig.mMaxRangeDayNum
                            ) {
                                mCalendarViewConfig.endRangeDate = selectTimestamp

                                mOnRangeClickListener()
                            }
                        }

                    }
                }


            }

        }



        if (!item?.day.isNullOrEmpty()) {
            val currentTimestamp =
                Utils.dateStrToTimestamp("${mYear}-${mMonth}-${item?.day}", "yyyy-MM-dd")

            if(mCalendarViewConfig.mIsOpenRange){
                if (mCalendarViewConfig.startRangeDate > 0 && mCalendarViewConfig.startRangeDate == currentTimestamp && mCalendarViewConfig.endRangeDate <= 0) {
                    binding.itemCalendarDay.setRoundRect(
                        mCalendarViewConfig.mRangeCorner,
                        mCalendarViewConfig.mRangeCorner,
                        mCalendarViewConfig.mRangeCorner,
                        mCalendarViewConfig.mRangeCorner,
                        mCalendarViewConfig.mDayRangeColor
                    )
                } else if (mCalendarViewConfig.endRangeDate > 0) {
                    if (mCalendarViewConfig.endRangeDate == mCalendarViewConfig.startRangeDate && mCalendarViewConfig.startRangeDate == currentTimestamp) {
                        binding.itemCalendarDay.setRoundRect(
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    } else if (mCalendarViewConfig.startRangeDate < mCalendarViewConfig.endRangeDate && mCalendarViewConfig.startRangeDate == currentTimestamp) {
                        binding.itemCalendarDay.setRoundRect(
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    } else if (mCalendarViewConfig.startRangeDate > mCalendarViewConfig.endRangeDate && mCalendarViewConfig.startRangeDate == currentTimestamp) {
                        binding.itemCalendarDay.setRoundRect(
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    } else if (mCalendarViewConfig.endRangeDate < mCalendarViewConfig.startRangeDate && mCalendarViewConfig.endRangeDate == currentTimestamp) {
                        binding.itemCalendarDay.setRoundRect(
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    } else if (mCalendarViewConfig.endRangeDate > mCalendarViewConfig.startRangeDate && mCalendarViewConfig.endRangeDate == currentTimestamp) {
                        binding.itemCalendarDay.setRoundRect(
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            0f,
                            mCalendarViewConfig.mRangeCorner,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    } else if (Utils.isTimeInterval(
                            mCalendarViewConfig.startRangeDate.toString(),
                            mCalendarViewConfig.endRangeDate.toString(),
                            currentTimestamp.toString()
                        )
                    ) {
                        binding.itemCalendarDay.setRoundRect(
                            0f,
                            0f,
                            0f,
                            0f,
                            mCalendarViewConfig.mDayRangeColor
                        )
                    }
                }

            }


            if (!mCalendarViewConfig.mPointColorDates.isNullOrEmpty()) {
                mCalendarViewConfig.mPointColorDates.entries.forEachIndexed { index, mutableEntry ->
                    if (mutableEntry.key == currentTimestamp) {
                        binding.itemCalendarDay.setPointColor(mutableEntry.value,mCalendarViewConfig.mDayPointWidthHeight)
                    }
                }
            }

        }

    }
}